package com.doradance.app.home;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doradance.R;
import com.doradance.account.UrlCenter;
import com.doradance.common.http.MyHttpRequest;
import com.doradance.entity.RankLearnEntity;
import com.pachong.android.baseuicomponent.activity.BaseActivity;
import com.pachong.android.framework.httprequest.responsecallbacks.DataRequestListener;
import com.pachong.android.frameworkbase.utils.EasyToast;
import com.pachong.android.frameworkbase.utils.GsonUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import de.hdodenhof.circleimageview.CircleImageView;

import static cn.jpush.android.api.JPushInterface.a.i;

public class RankLearnActivity extends BaseActivity {

    @Bind(R.id.civ_rank_2)
    CircleImageView mCivRank2;
    @Bind(R.id.iv_rank_2)
    ImageView mIvRank2;
    @Bind(R.id.tv_rank_name_2)
    TextView mTvRankName2;
    @Bind(R.id.tv_rank_time_2)
    TextView mTvRankTime2;
    @Bind(R.id.civ_rank_1)
    CircleImageView mCivRank1;
    @Bind(R.id.iv_rank_1)
    ImageView mIvRank1;
    @Bind(R.id.tv_rank_name_1)
    TextView mTvRankName1;
    @Bind(R.id.tv_rank_time_1)
    TextView mTvRankTime1;
    @Bind(R.id.civ_rank_3)
    CircleImageView mCivRank3;
    @Bind(R.id.iv_rank_3)
    ImageView mIvRank3;
    @Bind(R.id.tv_rank_name_3)
    TextView mTvRankName3;
    @Bind(R.id.tv_rank_time_3)
    TextView mTvRankTime3;
    @Bind(R.id.lv_rank)
    ListView mLvRank;
    ArrayList<RankLearnEntity> rankList = new ArrayList<RankLearnEntity>();
    @Bind(R.id.rl_rank_2)
    RelativeLayout mRlRank2;
    @Bind(R.id.rl_rank_1)
    RelativeLayout mRlRank1;
    @Bind(R.id.rl_rank_3)
    RelativeLayout mRlRank3;
    private RankAdapter rankAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
        initToolsBar();
    }

    private void initToolsBar() {
        setTitle(getString(R.string.learn_rank));
        getCustomToolbar().addLeftImageButton(R.mipmap.back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRlRank1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EasyToast.showToast(getApplicationContext(),getString(R.string.no_baby));
            }
        });
        mRlRank2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EasyToast.showToast(getApplicationContext(),getString(R.string.no_baby));
            }
        });
        mRlRank3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EasyToast.showToast(getApplicationContext(),getString(R.string.no_baby));
            }
        });
    }
    JSONObject jsonObject;
    JSONArray o2;
    List<RankLearnEntity> o;
    private void initData() {
        MyHttpRequest request = new MyHttpRequest(this);
        request.get(UrlCenter.RANK_LEARNING, null, new DataRequestListener<String>(String.class) {
            @Override
            public void onStart(Context context, String url) {
                super.onStart(context, url);
            }

            @Override
            public void onSuccess(String o1) {
                super.onSuccess(o1);
                try {
                    jsonObject = new JSONObject(o1);
                    o2 = (JSONArray) jsonObject.get("items");
                    Log.e("o2o2o2",o2.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                if(o2.toString().equals("[]")){
                }else{
                    try {
                        o=new ArrayList<RankLearnEntity>();
                        for(int i=0;i<o2.length();i++){
                            RankLearnEntity o3 = GsonUtils.string2Object(o2.get(i).toString(), RankLearnEntity.class);
                            o.add(o3);
                        }
                        if (o.size() == 1) {
                        setRankOne(o.get(0));
                    } else if (o.size() == 2) {
                        setRankOne(o.get(0));
                        setRankTwo(o.get(1));
                    } else {
                        setRankOne(o.get(0));
                        setRankTwo(o.get(1));
                        setRankThree(o.get(2));
                    }
                    if (o.size() > 3) {
                        for (int i = 0; i < o.size(); i++) {
                            if (i > 2) {
                                rankList.add(o.get(i));
                            }
                        }
                    }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    rankAdapter = new RankAdapter();
                    mLvRank.setAdapter(rankAdapter);
                    mLvRank.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            BabyDetailActivity.start(RankLearnActivity.this,rankList.get(position).getBaby_id());
                        }
                    });
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
            }
        });
    }


    private void setRankOne(final RankLearnEntity rankLearnEntity) {
        if(!TextUtils.isEmpty(rankLearnEntity.getAvatar())){
            Glide.with(this).load(rankLearnEntity.getAvatar()).dontAnimate().placeholder(R.mipmap.init_logo).into(mCivRank1);
        }else{
            Glide.with(this).load(R.mipmap.init_logo).into(mCivRank1);
        }
        if(!TextUtils.isEmpty(rankLearnEntity.getNick_name())){
            mTvRankName1.setText(rankLearnEntity.getNick_name());
        }else{
            mTvRankName1.setText(getString(R.string.default_name));
        }
        mTvRankName1.setVisibility(View.VISIBLE);
        mTvRankTime1.setVisibility(View.VISIBLE);

        mTvRankTime1.setText(rankLearnEntity.getHours() + "小时");
        mRlRank1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BabyDetailActivity.start(RankLearnActivity.this,rankLearnEntity.getBaby_id());
            }
        });
    }

    private void setRankTwo(final RankLearnEntity rankLearnEntity) {
        if(!TextUtils.isEmpty(rankLearnEntity.getAvatar())){
            Glide.with(this).load(rankLearnEntity.getAvatar()).dontAnimate().placeholder(R.mipmap.init_logo).into(mCivRank2);
        }else{
            Glide.with(this).load(R.mipmap.init_logo).into(mCivRank2);
        }
        if(!TextUtils.isEmpty(rankLearnEntity.getNick_name())){
            mTvRankName2.setText(rankLearnEntity.getNick_name());
        }else{
            mTvRankName2.setText(getString(R.string.default_name));
        }
        mTvRankName2.setVisibility(View.VISIBLE);
        mTvRankTime2.setVisibility(View.VISIBLE);
        mTvRankTime2.setText(rankLearnEntity.getHours() + "小时");
        mRlRank2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BabyDetailActivity.start(RankLearnActivity.this,rankLearnEntity.getBaby_id());
            }
        });
    }

    private void setRankThree(final RankLearnEntity rankLearnEntity) {
        if(!TextUtils.isEmpty(rankLearnEntity.getAvatar())){
            Glide.with(this).load(rankLearnEntity.getAvatar()).dontAnimate().placeholder(R.mipmap.init_logo).into(mCivRank3);
        }else{
            Glide.with(this).load(R.mipmap.init_logo).into(mCivRank3);
        }
        if(!TextUtils.isEmpty(rankLearnEntity.getNick_name())){
            mTvRankName3.setText(rankLearnEntity.getNick_name());
        }else{
            mTvRankName3.setText(getString(R.string.default_name));
        }
        mTvRankName3.setVisibility(View.VISIBLE);
        mTvRankTime3.setVisibility(View.VISIBLE);
        mTvRankTime3.setText(rankLearnEntity.getHours() + "小时");
        mRlRank3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BabyDetailActivity.start(RankLearnActivity.this,rankLearnEntity.getBaby_id());
            }
        });
    }


    public static void start(Activity activity) {
        Intent intent = new Intent(activity, RankLearnActivity.class);
        activity.startActivity(intent);
    }

    @Override
    public View createDataView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.activity_rank_learn, null);
    }


    class RankAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return rankList.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = getLayoutInflater();
            View view;

            if (convertView == null) {
                //因为getView()返回的对象，adapter会自动赋给ListView
                view = inflater.inflate(R.layout.rank_item, null);
            } else {
                view = convertView;
                Log.i("info", "有缓存，不需要重新生成" + position);
            }
            TextView tvRankItemNum = (TextView) view.findViewById(R.id.tv_rank_item_num);
            CircleImageView civItemHead = (CircleImageView) view.findViewById(R.id.civ_item_head);
            TextView tvItemName = (TextView) view.findViewById(R.id.tv_item_name);
            TextView tvItemTime = (TextView) view.findViewById(R.id.tv_item_time);
            tvRankItemNum.setText(position + 4 + "");
            Glide.with(RankLearnActivity.this).load(rankList.get(position).getAvatar()).dontAnimate().placeholder(R.mipmap.init_logo).into(civItemHead);
            tvItemName.setText(rankList.get(position).getNick_name());
            tvItemTime.setText(rankList.get(position).getHours() + "小时");
            return view;
        }
    }

}
